package com.fufan.mtsaas.modules.newtech.xjsphase.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.base.log.service.TSysLogSvc;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.newtech.xjsphase.bo.request.XjsPhaseQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjsphase.bo.request.XjsPhaseSaveRequest;
import com.fufan.mtsaas.modules.newtech.xjsphase.model.XjsPhase;
import com.fufan.mtsaas.modules.newtech.xjsphase.dao.XjsPhaseDao;
import com.fufan.mtsaas.modules.newtech.xjsphase.service.XjsPhaseSvc;

import com.fufan.mtsaas.modules.newtech.xjstech.model.XjsTech;
import com.fufan.mtsaas.modules.sysmgr.sygroup.bo.request.GroupMenuQueryRequest;
import com.fufan.mtsaas.util.DateFormatTool;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * <p>
 * 阶段主表 服务实现类
 * </p>
 *
 * @author Liby
 * @since 2024-12-16
 */
@Service("XjsPhaseSvcImpl")
@Slf4j
@Primary
public class XjsPhaseSvcImpl extends ServiceImpl<XjsPhaseDao, XjsPhase> implements XjsPhaseSvc {
    @Autowired
    private XjsPhaseDao entitymDao;

    @Autowired
    private TSysLogSvc tSysLogSvc;

    /**
     * 查询
     *
     * @param map
     * @return Result.class
     */
    @Override
    public Result doQueryPage(XjsPhaseQueryRequest map) {
        Result result = new Result();
        //分页查询阶段主表
        Page page = new Page();

        List<XjsPhase> entitys;

        return result;
    }

    /**
     * 新增
     *
     * @param entitym
     * @param requestPojo
     * @return Resultss
     */
    @Override
    @Transactional
    public Result doSave(XjsPhaseSaveRequest entitym, RequestPojo requestPojo) {
        Result<XjsPhase> result = new Result<>();
        try{
            entitym.setCreateTime(new Date());
            int rec = entitymDao.insert(entitym);
            log.debug("新增数据返回 {}",rec);

            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "xjsphase", "新增项目阶段", "项目名称:" + entitym.getName(), "新增", "成功", requestPojo);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        }catch (Exception e){
            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "xjsphase", "新增项目阶段", "项目名称:" + entitym.getName(), "新增", "失败", requestPojo);

            log.error("新增数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("新增数据出错：" + e.getMessage());
        }
        return result;
    }

    /**
     * 修改
     *
     * @param entitym
     * @param requestPojo
     * @return Result.class
     */
    @Override
    @Transactional
    public Result doUpdate(XjsPhaseSaveRequest entitym, RequestPojo requestPojo) {
        Result result = new Result();

        try{
            int rec = entitymDao.updateById(entitym);
            log.debug("更新数据返回 {}",rec);

            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "xjsphase", "修改项目阶段", "项目名称:" + entitym.getName(), "修改", "成功", requestPojo);

            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){
            tSysLogSvc.saveLogInfo(Long.valueOf(entitym.getId()), "xjsphase", "修改项目阶段", "项目名称:" + entitym.getName(), "修改", "失败", requestPojo);

            log.error("修改数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("修改数据出错：" + e.getMessage());
            // TransactionAspectSupport.currentTransactionStatus()().setRollbackOnly();
        }
        return result;

    }

    /**
     * 删除
     *
     * @param map
     * @return Result.class
     */
    @Override
    @Transactional
    public Result doDelete(XjsPhaseQueryRequest map, RequestPojo requestPojo) {
        Result result = new Result();

        try{
            int rec = entitymDao.deleteById(map.getId());

            tSysLogSvc.saveLogInfo(Long.valueOf(map.getId()), "xjsphase", "删除项目阶段", "项目名称:" + map.getName(), "删除", "成功", requestPojo);

            log.debug("删除数据返回 {}",rec);
            result.setResStatus(ResStatus.STATUS_SUCCESS);
            result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        }catch (Exception e){
            tSysLogSvc.saveLogInfo(Long.valueOf(map.getId()), "xjsphase", "删除项目阶段", "项目名称:" + map.getName(), "删除", "失败", requestPojo);

            log.error("删除数据出错",e);
            result.setResStatus(ResStatus.STATUS_DATABASE_FAIL);
            result.setResMsg("删除数据出错：" + e.getMessage());
            // TransactionAspectSupport.currentTransactionStatus()().setRollbackOnly();
        }
        return result;

    }
}
